// pages/news/news.js
const app = getApp();
import httpsUtils from '../../utils/https.js';
import commonUtils from '../../utils/common.js';
import myUtils from '../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders: ["综合排序", "发布时间", "价格最高", "价格最低", "收藏最多", "销量"],
    index: 0,
    isScreen: true,//显示遮罩
    showBussCircle: false,//显示商圈
    bussArray: [],
    bussList: [],//商圈列表前6个
    currentItemCircle: '',//双圈的id
    property: [], //选择属性
    onePropertyId: '',//一级属性的id
    value: '',//二级属性值
    showArray: [],//二级分类的id
    showArray1: [],//二级分类的false数组
    oneClassifyShow: false,//一级分类是否显示
    twoClassifyShow: false,//二级分类是否显示
    abc: true,
    showProperty: false,
    twoProperty: '',
    showSort: false,
    newsList: [], //一级分类的前6个
    newsList1: [], //剩余一级分类的数量
    oneTypeProperty: null,
    oneType: null,
    twoType: '',
    pxShowStatus: false,
    searchType: "1",
    pxName: "综合排序",
    pxUrl: "../../static/image/x_stoc/sanjiao.png",
    pxUrlAsc: "../../static/image/x_stoc/sanjiaoshang.png",
    current: 1,
    hasNextPage: true,
    newsGoodsList: [],
    newsGoodsLength: 1,
    isScroll: true,
    currentItem: ''
  },
  /*选择排序方式*/
  pxSelect: function (e) {
    var that = this;
    //再次点击关闭页面
    if (that.data.pxShowStatus) {
      that.setData({
        pxUrl: "../../static/image/x_stoc/sanjiao.png",
        pxShowStatus: false,
        searchType: e.currentTarget.dataset.searchType,
        pxName: e.currentTarget.dataset.searchTypeName,
        current: 1,
        hasNextPage: true,
        newsGoodsList: [],
      })
      /**刷新当前页面 */
      that.leftGoods();
    } else {
      that.setData({
        pxUrl: "../../static/image/x_stoc/sanjiaoshang.png",
        pxShowStatus: true
      })
    }
  },
  /**
   * 查询商品
   */
  leftGoods: function () {
    wx.showLoading({
      title: '加载中...',
    })
    var that = this;
    var searchType = that.data.searchType;
    /*选择分类*/
    httpsUtils.getData({
      _url: app.globalData._url + '/goods/selectGoodsListByCondition?isNew=1&current=' + that.data.current + "&sort=" + searchType,
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        that.setData({
          newsGoodsList: that.data.newsGoodsList.concat(json.data.list),//分类的列表
          current: json.data.pageNum,
          hasNextPage: json.data.hasNextPage,
          newsGoodsLength: json.data.total,
        })
      }
      wx.hideLoading();
    })
  },
  /*事件函数*/
  pickChange: function (e) {
    this.setData({
      index: e.detail.value
    });
  },

  showBussCircle: function (e) {
    this.setData({
      showBussCircle: !this.data.showBussCircle,
    })
  },
  /*点击商圈选中*/
  pitchOnBussCir: function (options) {
    var that = this
    var id = options.currentTarget.dataset.id;
    //设置当前样式

    that.setData({
      currentItem: id,
      currentItemCircle: id,

    })
  },
  /*点击一级属性选中*/
  onePropertyClick: function (options) {
    var that = this
    var id = options.currentTarget.dataset.id;
    var property = that.data.property;
    for (let obj of property) {
      if (obj.id == id) {
        obj.show = !obj.show;
      }
    }
    that.setData({
      property: property,
      pxUrlAsc: "../../static/image/x_stoc/sanjiaoshang.png"
    })
  },

  /*点击二级属性选中*/
  twoPropertyClick: function (options) {
    var that = this
    var id = options.currentTarget.dataset.id
    var oneTypeId = options.currentTarget.dataset.onetypeid
    var value = options.currentTarget.dataset.value;

    var property = that.data.property;
    for (let obj of property) {
      if (obj.id == oneTypeId) {
        for (let pro of obj.propertyTypeList) {
          if (pro.id == id) {
            pro.show = !pro.show;
          }
        }
      }
    }
    //设置当前样式
    that.setData({
      currentItem1: id,
      property: property,
    })
  },
  /*点击每次按钮跳转*/
  jumpFnn: function (options) {
    var that = this
    var id = options.currentTarget.dataset.id;
    //设置当前样式
    that.setData({
      currentItem: id,
      showBussCircle: false
    })
  },

  /*点击一级分类选中*/
  oneClassify: function (options) {
    var that = this;
    var id = options.currentTarget.dataset.id

    var newsList = that.data.newsList;
    for (let obj of newsList) {
      if (obj.id == id)
        obj.show = !obj.show;
    }
    that.setData({
      newsList: newsList,
    })
  },

  /*点击二级分类选中*/
  twoClassify: function (options) {
    var that = this
    var id = options.currentTarget.dataset.id
    var value = options.currentTarget.dataset.value;
    var oneTypeId = options.currentTarget.dataset.onetypeid

    var newsList = that.data.newsList;
    for (let obj of newsList) {
      if (obj.id == oneTypeId) {
        for (let pro of obj.sortTypeList) {
          if (pro.id == id) {
            pro.show = !pro.show;
          }
        }
      }
    }
    //设置当前样式
    that.setData({
      currentItem2: id,
      value: value,
      newsList: newsList,
    })
  },
  /*显示遮罩层*/
  conSelect: function (e) {
    this.setData({ isScreen: false, isScroll: false })
  },
  /**
   * 隐藏遮罩层
   */
  conNoSelect: function (e) {
    this.setData({ isScreen: true, isScroll: true })
  },
  reseting: function (e) {
    var that = this;
    var property = that.data.property;
    for (let obj of property) {
      obj.show = false;
      for (let pro of obj.propertyTypeList) {
        pro.show = false;
      }
    }
    var newsList = that.data.newsList;
    for (let obj of newsList) {
      obj.show = false;
      for (let pro of obj.sortTypeList) {
        pro.show = false;
      }
    }
    that.setData({
      oneType: '',
      oneTypeProperty: '',
      currentItemCircle: '',
      property: property,
      newsList: newsList,
      currentItem: ''
    })


  },
  ensure: function (e) {
    var that = this;
    var searchType = that.data.searchType;
    var propertyTypeId = that.data.twoProperty;
    var sortTypeId = that.data.twoType;
    var currentItemCircleId = that.data.currentItemCircle;

    var sortTypeIds = [];
    var newsList = that.data.newsList;
    for (let obj of newsList) {
      for (let pro of obj.sortTypeList) {
        if (pro.show) {
          sortTypeIds.push(pro.id);
        }
      }
    }

    var propertyTypeIds = [];
    var propertyList = that.data.property;
    for (let obj of propertyList) {
      for (let pro of obj.propertyTypeList) {
        if (pro.show) {
          propertyTypeIds.push(pro.id);
        }
      }
    }

    httpsUtils.getData({
      _url: app.globalData._url + '/goods/selectGoodsListByCondition?isNew=1&current=' + that.data.current + "&propertyTypeIds=" + myUtils.concatStr(propertyTypeIds, ',') + "&sortTypeIds=" + myUtils.concatStr(sortTypeIds, ',') + "&sort=" + searchType + "&tradeAreaId=" + currentItemCircleId,
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        that.setData({
          newsGoodsList: json.data.list,//分类的列表
          current: json.data.pageNum,
          hasNextPage: json.data.hasNextPage,
          newsGoodsLength: json.data.total,
        })
      }
      that.setData({
        isScreen: true,
        isScroll: true,
      })
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    /*选择商圈*/
    that.getData({
      _url: app.globalData._url + '/tradeArea/selectList',
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        that.setData({
          bussArray: json.data,
          bussList: json.data.splice(0, 7),
        })

      }
    })
    /*选择属性*/
    that.getData({
      _url: app.globalData._url + '/propertyType/selectTwoList',
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        for (let obj of json.data.list) {
          obj.show = false;
          for (let pro of obj.propertyTypeList) {
            pro.show = false;
          }
        }

        that.setData({
          property: json.data.list
        })

      }
    })
    /*选择分类*/
    that.getData({
      _url: app.globalData._url + '/sortType/selectTwoList',
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        for (let obj of json.data.list) {
          obj.show = false;
          for (let pro of obj.sortTypeList) {
            pro.show = false;
          }
        }
        that.setData({
          newsList: json.data.list,//分类的列表
        })
      }
    })

    /**刷新当前页面 */
    that.leftGoods();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.hasNextPage) {
      that.setData({
        current: that.data.current + 1,
      })
      /**刷新当前页面 */
      that.leftGoods();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getData: function (params, cb) {
    wx.request({
      url: params._url,
      data: params.data,
      method: 'GET',
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      success: function (res) {
        if (res.statusCode == 200) {
          typeof cb === 'function' && cb(res.data)
        }
      },
      fail: function () {
      },
      complete: function () {
      }
    })
  }
})